﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OSGeo.MapGuide.UI
{
    //TODO: Make FillView actually work!

    [DefaultProperty("WebLayoutResourceId")]
    public class MgWebLayout : Control
    {
        [Bindable(true)]
        [Category("General")]
        [Description("The AJAX viewer URL")]
        [DefaultValue("/mapguide/mapviewerajax")]
        [Localizable(true)]
        public string AjaxViewerUrl
        {
            get
            {
                String s = (String)ViewState["AjaxViewerUrl"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["AjaxViewerUrl"] = value;
            }
        }

        [Bindable(true)]
        [Category("General")]
        [Description("The Web Layout resource ID")]
        [DefaultValue("")]
        [Localizable(true)]
        public string WebLayoutResourceId
        {
            get
            {
                String s = (String)ViewState["WebLayoutResourceId"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["WebLayoutResourceId"] = value;
            }
        }

        [Bindable(true)]
        [Category("General")]
        [Description("Determines whether this viewer will fill 100% of its content area. If true, the width and height properties are ignored")]
        [DefaultValue(true)]
        [Localizable(true)]
        public bool FillView
        {
            get
            {
                object s = ViewState["FillView"];
                return ((s != null) ? (bool)s : false);
            }
            set
            {
                ViewState["FillView"] = value;
            }
        }

        [Bindable(true)]
        [Category("General")]
        [Description("The width of this viewer frame. Does nothing if FillView = true")]
        [DefaultValue(true)]
        [Localizable(true)]
        public string Width
        {
            get
            {
                object s = ViewState["Width"];
                return ((s != null) ? (string)s : "640px");
            }
            set
            {
                ViewState["Width"] = value;
            }
        }

        [Bindable(true)]
        [Category("General")]
        [Description("The height of this viewer frame. Does nothing if FillView = true")]
        [DefaultValue(true)]
        [Localizable(true)]
        public string Height
        {
            get
            {
                object s = ViewState["Height"];
                return ((s != null) ? (string)s : "480px");
            }
            set
            {
                ViewState["Height"] = value;
            }
        }

        [Browsable(false)]
        public string SessionID
        {
            get
            {
                object s = ViewState["SessionID"];
                return ((s != null) ? (string)s : "");
            }
            set
            {
                ViewState["SessionID"] = value;
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            string url = this.AjaxViewerUrl + "?WEBLAYOUT=" + this.WebLayoutResourceId;
            if (!string.IsNullOrEmpty(this.SessionID))
                url += "&SESSION=" + this.SessionID;

            if (this.FillView)
                output.Write("<table cellpadding='0' cellspacing='0' width='100%'><tr><td><iframe frameborder='0' src='" + url + "'></iframe></td></tr></table>");
            else
                output.Write("<iframe frameborder='0' width='" + this.Width + "' height='" + this.Height + "' src='" + url + "'></iframe>");
        }
    }
}
